package router

import (
	"github.com/gin-gonic/gin"
	"import-master/handler"
	"net/http"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {

	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello World!")
	})
	r.POST("/userLogin", handler.UserLogin)      //用户登录
	r.POST("/userRole", handler.UserRole)        //用户和角色关联
	r.POST("/RoleNode", handler.RoleNode)        //角色和权限关联
	r.GET("/userRoleList", handler.UserRoleList) //用户对于的角色
	r.GET("/RoleNodeList", handler.RoleNodeList) //角色对于的权限
	r.GET("/userDetail", handler.UserDetail)     //分页列表
	r.GET("/DetailList", handler.DetailList)     //列表详情
	return r
}
